home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1110 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  99 lines

  1. Path: gwaihir.isoft.com.ar!pablog
  2. From: pablog@gwaihir.isoft.com.ar (Pablo Hernan Gelaf)
  3. Newsgroups: gnu.g++.help,comp.lang.c++
  4. Subject: Templates & inlines
  5. Date: 9 Jan 1996 07:05:12 -0500
  6. Organization: Gatewayed from the GNU Project mailing list help-g++@prep.ai.mit.edu
  7. Sender: daemon@cis.ohio-state.edu
  8. Distribution: world
  9. Message-ID: <m0tZc7J-000CBoC@gizmo>
  10. Reply-To: pablog@isoft.com.ar
  11.  
  12. I have the following code that implements arithmetic operators using
  13. templates;
  14.  
  15. --------8<--------------------------------------------------
  16. #include <stdio.h>
  17.  
  18. class A {
  19.  
  20. private:
  21.     
  22.     int i;
  23.         
  24. public:
  25.  
  26.     A(int j)
  27.     :    i(j)
  28.     { }
  29.     
  30.     friend inline int cmp(const A &a1, const A &a2);
  31. };
  32.  
  33. inline int cmp(const A &a1, const A &a2)
  34. {
  35.     return a1.i - a2.i;
  36. }
  37.  
  38. template<class T> inline bool operator==(const T &op1, const T &op2)
  39. {
  40.     return cmp(op1, op2) == 0;
  41. }
  42.  
  43. template<class T> bool operator<(const T &op1, const T &op2)
  44. {
  45.     return cmp(op1, op2) < 0;
  46. }
  47.  
  48. template<class T> bool operator>(const T &op1, const T &op2)
  49. {
  50.     return cmp(op1, op2) > 0;
  51. }
  52.  
  53. template<class T> bool operator<=(const T &op1, const T &op2)
  54. {
  55.     return cmp(op1, op2) <= 0;
  56. }
  57.  
  58. template<class T> bool operator>=(const T &op1, const T &op2)
  59. {
  60.     return cmp(op1, op2) >= 0;
  61. }
  62.  
  63. int main(int argc, char **argv)
  64. {
  65.     A a1(1), a2(2);
  66.     
  67.     for (int i = 0; i < 10000000; i++)
  68.         if (a1 == a2)
  69.             fprintf(stderr, "a1 == a2\n");
  70.     
  71. }
  72. --------8<--------------------------------------------------
  73.  
  74. Comparing (in term of time) this implementation with another that 
  75. does not use templates I can figure that the 'operator==' is not being 
  76. compiled as an inline function. I'm compiling on a SGI (IRIX 5.3)
  77. using gcc2.7.0 with the '-O2' flag.
  78.  
  79. Is there any flag that I can use to turn on the inline capability in 
  80. templates functions ?
  81.  
  82. Thanks.
  83. -- 
  84. -----------------------------------------------------------------
  85. Pablo H. Gelaf                           InterSoft Argentina S.A.
  86. Senior Software Engineer                  Av. Cordoba 883 Piso 13
  87. pablog@isoft.com.ar               (1054) Buenos Aires - Argentina
  88. http://www.isoft.com.ar/~pablog             Voice: +54-1-318-8900
  89.                                               Fax: +54-1-318-8999
  90. -----------------------------------------------------------------
  91. -- 
  92. -----------------------------------------------------------------
  93. Pablo H. Gelaf                           InterSoft Argentina S.A.
  94. Senior Software Engineer                  Av. Cordoba 883 Piso 13
  95. pablog@isoft.com.ar               (1054) Buenos Aires - Argentina
  96. http://www.isoft.com.ar/~pablog             Voice: +54-1-318-8900
  97.                                               Fax: +54-1-318-8999
  98. -----------------------------------------------------------------
  99.